import time

import requests
from util.get_header import get_headers_init_referer
from util.get_time_stamp import get_time_stamp
import json
# headers.update = get_headers_init_user_agent()





response = requests.get(f'https://product.weather.com.cn/alarm/grepalarm_cn.php?_={get_time_stamp()}', headers=get_headers_init_referer('https://www.weather.com.cn/'))
# print(response.status_code)

content = response.content.decode('utf-8')[14:-1]
data = json.loads(content)
time.sleep(1)
for item in data['data']:
    # print(item)
    title = item[0]
    detail_url =f'https://product.weather.com.cn/alarm/webdata/{item[1]}?_={get_time_stamp()}'

    detail_response = requests.get(detail_url, headers=get_headers_init_referer('https://www.weather.com.cn/'))
    detail_data = json.loads(detail_response.content.decode('utf-8')[14:])
    # print(detail_data)
    # PROVINCE = detail_data["PROVINCE"]
    # CITY = detail_data["CITY"]
    # STATIONNAME = detail_data["STATIONNAME"]
    # SIGNALTYPE = detail_data["SIGNALTYPE"]
    HEAD = detail_data["head"]
    # print(PROVINCE)
    with open('static/天气预报','w',encoding='utf-8') as f:
        f.write(HEAD)
    # break
